﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>StrSplit - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The StrSplit function separates a string into an array of substrings using the specified delimiters." />
<meta name="ahk:equiv-v1" content="commands/StringSplit.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>StrSplit</h1>

<p>Separates a string into an <a href="../Objects.htm#Usage_Simple_Arrays">array</a> of substrings using the specified delimiters.</p>

<pre class="Syntax">Array := <span class="func">StrSplit</span>(String <span class="optional">, Delimiters, OmitChars, MaxParts := -1</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>String</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>A string to split.</p>
  </dd>

  <dt>Delimiters</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>If this parameter is blank or omitted, each character of the input string will be treated as a separate substring.</p>
    <p>Otherwise, <em>Delimiters</em> can be either a single string or an array of strings, each of which is used to determine where the boundaries between substrings occur. Since the delimiters are not considered to be part of the substrings themselves, they are never included in the returned array. Also, if there is nothing between a pair of delimiters within the input string, the corresponding array element will be blank.</p>
    <p>例如: <code>&quot;,&quot;</code> would divide the string based on every occurrence of a comma. Similarly, <code>[A_Tab, A_Space]</code> would create a new array element every time a space or tab is encountered in the input string.</p>
  </dd>

  <dt>OmitChars</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>An optional list of characters (case sensitive) to exclude from the beginning and end of each array element. 例如, if <em>OmitChars</em> is <code>&quot; `t&quot;</code>, spaces and tabs will be removed from the beginning and end (but not the middle) of every element.</p>
    <p>If <em>Delimiters</em> is blank, <em>OmitChars</em> indicates which characters should be excluded from the array.</p>
  </dd>
  
  <dt>MaxParts</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The maximum number of substrings to return. If non-zero, the string is split a maximum of <em>MaxParts</em>-1 times and the remainder of the string is returned in the last substring (excluding any leading or trailing <em>OmitChars</em>). If omitted or -1, there is no limit.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#objects">数组</a></p>
<p>This function returns an array containing the substrings of the specified string.</p>

<h2 id="Remarks">备注</h2>
<p>Whitespace characters such as spaces and tabs will be preserved unless those characters are included in the <em>Delimiters</em> or <em>OmitChars</em> parameters. Tabs and spaces can be trimmed from both ends of any variable by using <a href="Trim.htm">Trim</a>. 例如: <code>Var := Trim(Var)</code></p>
<p>To split a string that is in standard CSV (comma separated value) format, use a <a href="LoopParse.htm">parsing loop</a> since it has  built-in CSV handling.</p>
<p>To arrange the fields in a different order prior to splitting them, use the <a href="Sort.htm">Sort</a> function.</p>
<p>If you do not need the substrings to be permanently stored in memory, consider using a <a href="LoopParse.htm">parsing loop</a> -- especially if the input string is very large, in which case a large amount of memory would be saved. 例如:</p>
<pre>Colors := "red,green,blue"
Loop Parse, Colors, ","
    MsgBox "Color number " A_Index " is " A_LoopField</pre>
<h2 id="Related">相关</h2>
<p><a href="LoopParse.htm">Parsing loop</a>, <a href="../misc/Arrays.htm">Arrays</a>, <a href="Sort.htm">Sort</a>, <a href="SplitPath.htm">SplitPath</a>, <a href="InStr.htm">InStr</a>, <a href="SubStr.htm">SubStr</a>, <a href="StrLen.htm">StrLen</a>, <a href="StrLower.htm">StrLower</a>, <a href="StrLower.htm">StrUpper</a>, <a href="StrReplace.htm">StrReplace</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>TestString := "This is a test."
word_array := StrSplit(TestString, A_Space, ".")  <em>; Omits periods.</em>
MsgBox("The 4th word is " word_array[4])</pre>
</div>

<div class="ex" id="ExForLoop">
<p><a href="#ExForLoop">#2</a></p>
<pre>Colors := "red,green,blue"
ColorArray := StrSplit(Colors, ",")
Loop ColorArray.Length()
{
    this_color := ColorArray[a_index]
    MsgBox "Color number " a_index " is " this_color
}</pre>
</div>

</body>
</html>